package com.ljk;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

class ExampleClass {
    int nonGenericField;
    List<String> genericField;
}

public class TEST2 {
    public static void main(String[] args) throws NoSuchFieldException {
        // 获取非泛型字段的类型信息
        Field nonGenericField = ExampleClass.class.getDeclaredField("nonGenericField");
        Type nonGenericType = nonGenericField.getGenericType();
        System.out.println("Non-generic field type: " + nonGenericType);

        // 获取泛型字段的类型信息
        Field genericField = ExampleClass.class.getDeclaredField("genericField");
        Type genericType = genericField.getGenericType();
        System.out.println("Generic field type: " + genericType);

        // 如果是参数化类型，进一步获取原始类型和类型参数
        if (genericType instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) genericType;
            Type rawType = parameterizedType.getRawType();
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            System.out.println("Raw type: " + rawType);
            System.out.println("Actual type arguments:");
            for (Type typeArgument : actualTypeArguments) {
                System.out.println("  - " + typeArgument);
            }
        }
    }
}
